Power Set Algorithm
The Power Set Algorithm is a technique used in computer science and mathematics to generate all possible subsets of a given set. This algorithm is widely used in various applications such as combinatorics, graph theory, and optimization problems. In essence, the power set of a set is a collection of all possible subsets, including the empty set and the set itself. For instance, given a set S = {a, b, c}, its power set P(S) would be {{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}. The size of the power set is always 2^n, where n is the number of elements in the original set.
There are several ways to implement the Power Set Algorithm, including recursive, iterative, and bit manipulation-based methods. One common recursive approach is to generate the subsets by adding the current element to all the subsets generated without it. In the iterative method, we loop through the elements and add them to the existing subsets. A more efficient and concise way to generate the power set is using bit manipulation, where we represent subsets as binary strings of length n, with the presence or absence of an element indicated by a 1 or 0 at the corresponding index. For example, in the set S = {a, b, c}, the binary string 101 represents the subset {a, c}. By iterating through all the binary strings of length n, we can generate all possible subsets of the given set.
/*
Petar 'PetarV' Velickovic
Algorithm: Power Set
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
using namespace std;
typedef long long lld;
//Metoda koja generise partitivni skup nekog skupa
//Slozenost: O(2^n)
int n;
int skup[100];
bool inSet[100];
void powerSet(int pos)
{
if (pos==n)
{
for (int i=0;i<n;i++) if (inSet[i]) printf("%d ",skup[i]);
printf("\n");
return;
}
inSet[pos] = false;
powerSet(pos+1);
inSet[pos] = true;
powerSet(pos+1);
}
int main()
{
n = 3;
skup[0] = 1;
skup[1] = 2;
skup[2] = 3;
powerSet(0);
return 0;
}